package com.hxgz.steel.controller.portal;

import com.github.pagehelper.PageInfo;
import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.dto.OrderDetailDto;
import com.hxgz.steel.dto.SellerOrderDto;
import com.hxgz.steel.dto.WXorderDto;
import com.hxgz.steel.service.ConsumerCenterService;
import com.hxgz.steel.service.SellerOrderService;
import com.hxgz.steel.util.ExtractStringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 卖家订单
 */
@RestController
@RequestMapping("sellerOrder")
@Api(tags = "SellerOrderController", description = "前台-卖家中心-订单管理")
public class SellerOrderController {

    @Autowired
    SellerOrderService soService;
    @Autowired
    private ConsumerCenterService consumerCenterService;

    /**
     * 列表
     *
     * @param status 状态
     * @param page
     * @return
     */
    @RequestMapping(value = "listSellerOrders.do", method = RequestMethod.POST)
    @ApiOperation("订单管理-列表")
    public ServerResponse listSellerOrders(@RequestParam(required = false) @ApiParam("下单开始时间") String payMentTimeStr, @RequestParam(required = false) @ApiParam("下单截止时间") String payMentTimeEnd,
                                           @RequestParam(required = false) @ApiParam("订单状态  0待付款 1 待发货 2已发货 3已完成 4已取消 5已关闭") Integer status, Page page, HttpServletRequest httpServletRequest) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        SellerOrderDto dto = new SellerOrderDto();
        dto.setStatus(status);
        dto.setPayMentTimeStr(payMentTimeStr);
        dto.setPayMentTimeEnd(payMentTimeEnd);
        return soService.listSellerOrders(dto, page, user.getId());
    }

    /**
     * 订单详情
     *
     * @param id 订单id
     * @return
     */
    @RequestMapping(value = "getOrderInfo.do", method = RequestMethod.POST)
    @ApiOperation(value = "订单管理-订单详情")
    public ServerResponse<OrderDetailDto> getOrderInfo(Long id, HttpServletRequest httpServletRequest) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return soService.getOrderInfo(id, user.getId());
    }

    @ApiOperation("订单管理-取消订单")
    @RequestMapping(value = "cancelOrder.do", method = RequestMethod.POST)
    public ServerResponse cancelOrder(Long id, HttpServletRequest httpServletRequest) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return consumerCenterService.updateOrderStatus(id, 4, user.getId());
    }

    @ApiOperation("订单管理-填写物流单号")
    @RequestMapping(value = "writeLogisticsNum.do", method = RequestMethod.POST)
    public ServerResponse writeLogisticsNum(@RequestParam(required = true) @ApiParam("订单id") Long id, @RequestParam(required = true) @ApiParam("物流公司") String logistics,
                                            @RequestParam(required = true) @ApiParam("订单编号") String logisticsNum, HttpServletRequest httpServletRequest) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        if (!ExtractStringUtils.isPositiveNumber(logisticsNum)){
            return ServerResponse.createByErrorMessage("请输入纯数字的物流单号");
        }
        return soService.writeLogisticsNum(id, logistics, logisticsNum);
    }


    @ApiOperation("订单管理-删除订单")
    @RequestMapping(value = "deleteOrder.do", method = RequestMethod.POST)
    public ServerResponse deleteOrder(Long id, HttpServletRequest httpServletRequest) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return consumerCenterService.updateOrderStatus(id, 6, user.getId());
    }

    @ApiOperation("订单管理-已送货")
    @RequestMapping(value = "delivered.do", method = RequestMethod.POST)
    public ServerResponse delivered(Long id, HttpServletRequest httpServletRequest, String logisticsNum) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        //商家自己送货的快递公司名称为QSH,以区别于快递公司
        return soService.writeLogisticsNum(id, "QSH", logisticsNum);
    }

    @RequestMapping(value = "WXlistSellerOrders.do", method = RequestMethod.POST)
    @ApiOperation("(小程序端)商城订单管理-列表")
    public ServerResponse<PageInfo<List<WXorderDto>>> WXlistSellerOrders(@RequestParam(required = false) @ApiParam("订单状态  0待付款 1 待发货 3已完成 4已取消") Integer status, HttpServletRequest httpServletRequest,
                                                                         Page page) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return soService.WXlistSellerOrders(status, user.getId(), page);
    }

    @RequestMapping(value = "WXBuySellerOrders.do", method = RequestMethod.POST)
    @ApiOperation("(小程序端)商城求购订单管理-列表")
    public ServerResponse<PageInfo<List<WXorderDto>>> WXBuySellerOrders(@RequestParam(required = false) @ApiParam("订单状态  0待付款 1 待发货 3已完成 4已取消") Integer status, HttpServletRequest httpServletRequest,
                                                                        Page page) {
        User user = Const.checkUserLogin(httpServletRequest);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return soService.wxBuySellerOrders(status, user.getId(), page);
    }

    @ApiOperation("订单管理-填写物流单号:物流公司选择")
    @RequestMapping(value = "logisticsCompanyListChange.do", method = RequestMethod.POST)
    public ServerResponse logisticsCompanyListChange(HttpServletRequest httpServletRequest) {
        return soService.logisticsCompanyListChange();
    }

    @ApiOperation("(小程序)微信自营商城订单详情")
    @RequestMapping(value = "wxSelfOrderInformation.do", method = RequestMethod.POST)
    public ServerResponse<OrderDetailDto> wxOrderInformation(HttpServletRequest request, @RequestParam @ApiParam("订单id") Long id) {
        User user = Const.checkUserLogin(request);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return soService.wxSelfOrderInformation(user.getId(), id);
    }


    @ApiOperation("新增商品评论")
    @RequestMapping(value = "addProductComments.do", method = RequestMethod.POST)
    public ServerResponse addProductComments(HttpServletRequest request, @ApiParam("订单id") @RequestParam(required = false) Long orderId, @ApiParam("评论内容") @RequestParam(required = false) String comment,
                                             @ApiParam("评分（星星数）0没星 1-1颗星 2-2颗 3-3颗 4-4颗 5-5颗") @RequestParam Integer grade) {
        User user = Const.checkUserLogin(request);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return soService.addProductComment(orderId, user.getId(), comment, grade);
    }

    @ApiOperation("订单评论内容")
    @RequestMapping(value = "showProductComment.do", method = RequestMethod.POST)
    public ServerResponse showProductComment(HttpServletRequest request, @ApiParam("订单id") @RequestParam Long orderId) {
        User user = Const.checkUserLogin(request);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        return soService.showProductComment(orderId, user.getId());
    }
}
